package com.esri.android.map.ogc;

import android.util.Log;
import com.esri.android.map.GroupLayer;
import com.esri.android.map.ags.ArcGISFeatureLayer;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.Polygon;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.internal.a.b.c;
import com.esri.core.internal.value.KMLFeatureInfo;
import com.esri.core.internal.value.d;
import com.esri.core.map.CallbackListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/* loaded from: classes.dex */
public class KMLLayer extends GroupLayer {
    final AtomicBoolean A;
    boolean B;
    c b;
    AtomicBoolean c;
    ArcGISFeatureLayer d;
    ArcGISFeatureLayer e;
    ArcGISFeatureLayer f;
    d g;
    Timer s;
    String t;
    int u;
    List<d> v;
    List<KMLLayer> w;
    List<com.esri.android.map.ogc.b> x;
    KMLLayer y;
    final AtomicInteger z;

    public KMLLayer(String str) {
        this(str, SpatialReference.create(4326));
    }

    public KMLLayer(String str, SpatialReference spatialReference) {
        this(str, spatialReference, null, true);
    }

    public KMLLayer(String str, SpatialReference spatialReference, boolean z) {
        this(str, spatialReference, null, z);
    }

    KMLLayer(String str, SpatialReference spatialReference, d dVar, boolean z) {
        super(false);
        this.b = null;
        this.c = new AtomicBoolean(false);
        this.g = null;
        this.v = new ArrayList();
        this.w = new ArrayList();
        this.x = null;
        this.y = null;
        this.z = new AtomicInteger(0);
        this.A = new AtomicBoolean(false);
        this.B = false;
        setUrl(str);
        setDefaultSpatialReference(spatialReference);
        this.g = dVar;
        if (dVar != null) {
            setName(dVar.g());
        }
        if (z) {
            try {
                getServiceExecutor().submit(new Runnable() { // from class: com.esri.android.map.ogc.KMLLayer.1
                    @Override // java.lang.Runnable
                    public void run() {
                        KMLLayer.this.initLayer();
                    }
                });
            } catch (RejectedExecutionException e) {
                e.printStackTrace();
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.esri.android.map.Layer
    public void updateSpatialReferenceStatus(int i) {
        super.updateSpatialReferenceStatus(i);
        if (i > 0) {
            c();
        }
    }

    void c() {
        if (this.z.get() == 2 || this.z.compareAndSet(1, 2)) {
            h();
            if (this.B) {
                return;
            }
            i();
            this.B = true;
            return;
        }
        this.z.compareAndSet(0, 1);
    }

    public void refresh() {
        try {
            getServiceExecutor().submit(new Runnable() { // from class: com.esri.android.map.ogc.KMLLayer.2
                @Override // java.lang.Runnable
                public void run() {
                    KMLLayer.this.d();
                }
            });
        } catch (RejectedExecutionException unused) {
        }
    }

    void d() {
        if (this.z.get() == 2 && this.A.compareAndSet(false, true)) {
            this.d = null;
            this.e = null;
            this.f = null;
            this.x = null;
            this.w = new ArrayList();
            this.v = new ArrayList();
            removeAll();
            d dVar = this.g;
            if (dVar != null && dVar.c() != null) {
                setUrl(a(this.g));
            }
            try {
                e();
            } catch (Exception e) {
                Log.e(com.esri.core.internal.a.a, "KML layer fails to refresh" + e);
            }
            this.A.set(false);
        }
    }

    @Override // com.esri.android.map.GroupLayer, com.esri.android.map.Layer
    protected void initLayer() {
        this.A.set(true);
        try {
            removeAll();
            e();
            changeStatus(OnStatusChangedListener.STATUS.INITIALIZED);
            this.z.set(1);
            c();
        } catch (Exception e) {
            this.z.set(0);
            Log.e(com.esri.core.internal.a.a, "KML layer fails to initialize" + e);
            changeStatus(OnStatusChangedListener.STATUS.fromInt(OnStatusChangedListener.EsriStatusException.INIT_FAILED_KML_LAYER));
        }
        this.A.set(false);
    }

    void e() throws Exception {
        this.b = new com.esri.core.internal.a.b.a(new com.esri.core.internal.a.b.b(getUrl(), null, getDefaultSpatialReference()), new b()).execute();
        this.c.set(true);
        if (this.b.a() != null && this.b.a().size() > 0 && this.serviceInfo != null) {
            List list = (List) this.serviceInfo;
            for (com.esri.core.internal.value.b bVar : this.b.a()) {
                if (list.contains(Integer.valueOf(bVar.c()))) {
                    bVar.a(true);
                } else {
                    bVar.a(false);
                }
            }
        }
        g();
        ArcGISFeatureLayer arcGISFeatureLayer = this.d;
        if (arcGISFeatureLayer != null) {
            arcGISFeatureLayer.setVisible(true);
        }
        ArcGISFeatureLayer arcGISFeatureLayer2 = this.e;
        if (arcGISFeatureLayer2 != null) {
            arcGISFeatureLayer2.setVisible(true);
        }
        ArcGISFeatureLayer arcGISFeatureLayer3 = this.f;
        if (arcGISFeatureLayer3 != null) {
            arcGISFeatureLayer3.setVisible(true);
        }
        c();
    }

    private void h() {
        List<d> list = this.v;
        if (list == null || list.size() <= 0) {
            return;
        }
        try {
            if (getSpatialReference().getLatestID() != getDefaultSpatialReference().getLatestID()) {
                return;
            }
            for (d dVar : this.v) {
                String a2 = a(dVar);
                if (a2 != null) {
                    addLayer(new KMLLayer(a2, getDefaultSpatialReference(), dVar, true));
                }
            }
        } catch (Exception e) {
            Log.e(com.esri.core.internal.a.a, "Network link fails to initialize", e);
        }
    }

    private String a(d dVar) {
        String str;
        String d = dVar.d();
        if (dVar.d() == null || dVar.c() == null || dVar.c().length() <= 0) {
            str = null;
        } else {
            String c = dVar.c();
            int a2 = dVar.a();
            Polygon extent = getExtent();
            Envelope envelope = new Envelope();
            extent.queryEnvelope(envelope);
            Envelope envelope2 = getSpatialReference().getLatestID() == 4326 ? envelope : (Envelope) GeometryEngine.project(envelope, getSpatialReference(), SpatialReference.create(4326));
            Point center = envelope2.getCenter();
            if (getSpatialReference().getLatestID() != 3857) {
                envelope = (Envelope) GeometryEngine.project(envelope, getSpatialReference(), SpatialReference.create(3557));
            }
            double max = Math.max(envelope.getWidth(), envelope.getHeight());
            if (a2 != -1) {
                double width = envelope2.getWidth();
                double d2 = a2;
                Double.isNaN(d2);
                double width2 = (width * d2) - envelope2.getWidth();
                double height = envelope2.getHeight();
                Double.isNaN(d2);
                envelope2.inflate(width2 / 2.0d, ((height * d2) - envelope2.getHeight()) / 2.0d);
            }
            str = c.replace("[bboxWest]", envelope2.getXMin() < -180.0d ? "-180" : Double.toString(envelope2.getXMin())).replace("[bboxEast]", envelope2.getXMax() > 180.0d ? "180" : Double.toString(envelope2.getXMax())).replace("[bboxSouth]", envelope2.getYMin() < -90.0d ? "-90" : Double.toString(envelope2.getYMin())).replace("[bboxNorth]", envelope2.getYMax() > 90.0d ? "90" : Double.toString(envelope2.getYMax())).replace("[lookatLon]", Double.toString(center.getX())).replace("[lookatLat]", Double.toString(center.getY())).replace("[lookatRange]", Double.toString(max)).replace("[lookatTilt]", "0").replace("[lookatHeading]", "0").replace("[lookatTerrainLon]", Double.toString(center.getX())).replace("[lookatTerrainLat]", Double.toString(center.getY())).replace("[lookatTerrainAlt]", "0").replace("cameraLon", Double.toString(center.getX())).replace("[cameraLat]", Double.toString(center.getY())).replace("[cameraAlt]", Double.toString(max)).replace("[horizFov]", "60").replace("[vertFov]", "60").replace("[horizPixels]", Integer.toString(getWidth())).replace("[vertPixels]", Integer.toString(getHeight())).replace("[terrainEnabled]", "0");
        }
        String replace = dVar.e() != null ? dVar.e().replace("[clientVersion]", "3.0").replace("[kmlVersion]", "2.3").replace("[clientName]", "ArcGIS_for_Android").replace("[language]", "en-us") : null;
        StringBuilder sb = new StringBuilder();
        if (d == null) {
            d = "";
        }
        sb.append(d);
        if (str != null) {
            sb.append("?");
            sb.append(str);
        }
        if (replace != null) {
            sb.append("&");
            sb.append(replace);
        }
        return sb.toString();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* renamed from: com.esri.android.map.ogc.KMLLayer$4  reason: invalid class name */
    /* loaded from: classes.dex */
    public static /* synthetic */ class AnonymousClass4 {
        static final /* synthetic */ int[] a;

        static {
            int[] iArr = new int[KMLFeatureInfo.FEATURETYPE.values().length];
            a = iArr;
            try {
                iArr[KMLFeatureInfo.FEATURETYPE.FOLDER.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                a[KMLFeatureInfo.FEATURETYPE.GROUND_OVERLAY.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                a[KMLFeatureInfo.FEATURETYPE.SCREEN_OVERLAY.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                a[KMLFeatureInfo.FEATURETYPE.NETWORK_LINK.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                a[KMLFeatureInfo.FEATURETYPE.POINT.ordinal()] = 5;
            } catch (NoSuchFieldError unused5) {
            }
            try {
                a[KMLFeatureInfo.FEATURETYPE.LINE.ordinal()] = 6;
            } catch (NoSuchFieldError unused6) {
            }
            try {
                a[KMLFeatureInfo.FEATURETYPE.POLYGON.ordinal()] = 7;
            } catch (NoSuchFieldError unused7) {
            }
        }
    }

    Object a(KMLFeatureInfo kMLFeatureInfo) {
        List<KMLLayer> list;
        if (kMLFeatureInfo == null) {
            return null;
        }
        int a2 = kMLFeatureInfo.a();
        int i = AnonymousClass4.a[kMLFeatureInfo.b().ordinal()];
        if (i == 1) {
            List<com.esri.core.internal.value.b> a3 = this.b.a();
            if (a3 != null) {
                for (com.esri.core.internal.value.b bVar : a3) {
                    if (bVar.c() == a2) {
                        return bVar;
                    }
                }
            }
        } else if (i == 2) {
            List<com.esri.android.map.ogc.b> list2 = this.x;
            if (list2 != null) {
                for (com.esri.android.map.ogc.b bVar2 : list2) {
                    com.esri.android.map.ogc.a[] c = bVar2.c();
                    if (c != null && c.length > 0 && ((a) c[0]).h() == kMLFeatureInfo.a()) {
                        return bVar2;
                    }
                }
            }
        } else if (i == 4 && (list = this.w) != null) {
            for (KMLLayer kMLLayer : list) {
                d dVar = kMLLayer.g;
                if (dVar != null && dVar.f() == kMLFeatureInfo.a()) {
                    return kMLLayer;
                }
            }
        }
        return null;
    }

    com.esri.core.internal.value.b[] f() {
        return (com.esri.core.internal.value.b[]) this.b.a().toArray(new com.esri.core.internal.value.b[0]);
    }

    void g() {
        com.esri.core.internal.value.b[] f = f();
        if (f == null || f.length <= 0) {
            return;
        }
        com.esri.core.internal.value.b bVar = f[0];
        a(bVar, bVar.h());
    }

    private void a(com.esri.core.internal.value.b bVar, boolean z) {
        int intValue;
        if (!isInitialized() || bVar == null || bVar.b() == null) {
            return;
        }
        Map<Integer, Integer> map = this.b.b().get(Geometry.Type.POINT);
        Map<Integer, Integer> map2 = this.b.b().get(Geometry.Type.POLYLINE);
        Map<Integer, Integer> map3 = this.b.b().get(Geometry.Type.POLYGON);
        for (KMLFeatureInfo kMLFeatureInfo : bVar.b()) {
            int i = AnonymousClass4.a[kMLFeatureInfo.b().ordinal()];
            boolean z2 = false;
            if (i == 1) {
                com.esri.core.internal.value.b bVar2 = null;
                List<com.esri.core.internal.value.b> a2 = this.b.a();
                if (a2 != null) {
                    Iterator<com.esri.core.internal.value.b> it = a2.iterator();
                    while (true) {
                        if (!it.hasNext()) {
                            break;
                        }
                        com.esri.core.internal.value.b next = it.next();
                        if (next.c() == kMLFeatureInfo.a()) {
                            bVar2 = next;
                            break;
                        }
                    }
                }
                if (bVar2 != null) {
                    if (z && bVar2.h()) {
                        z2 = true;
                    }
                    a(bVar2, z2);
                }
            } else if (i == 2) {
                com.esri.android.map.ogc.b bVar3 = (com.esri.android.map.ogc.b) a(kMLFeatureInfo);
                if (bVar3 != null) {
                    if (z && bVar3.isVisible()) {
                        z2 = true;
                    }
                    bVar3.setVisible(z2);
                }
            } else if (i == 4) {
                KMLLayer kMLLayer = (KMLLayer) a(kMLFeatureInfo);
                if (kMLLayer != null) {
                    if (z && kMLLayer.isVisible()) {
                        z2 = true;
                    }
                    kMLLayer.setVisible(z2);
                }
            } else if (i == 5) {
                int intValue2 = map.get(Integer.valueOf(kMLFeatureInfo.a())).intValue();
                if (intValue2 != -1) {
                    this.d.setGraphicVisible(intValue2, z);
                }
            } else if (i == 6) {
                int intValue3 = map2.get(Integer.valueOf(kMLFeatureInfo.a())).intValue();
                if (intValue3 != -1) {
                    this.e.setGraphicVisible(intValue3, z);
                }
            } else if (i == 7 && (intValue = map3.get(Integer.valueOf(kMLFeatureInfo.a())).intValue()) != -1) {
                this.f.setGraphicVisible(intValue, z);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public class b implements CallbackListener<Object> {
        @Override // com.esri.core.map.CallbackListener
        public void onError(Throwable th) {
        }

        b() {
        }

        @Override // com.esri.core.map.CallbackListener
        public void onCallback(Object obj) {
            if (obj instanceof ArcGISFeatureLayer) {
                ArcGISFeatureLayer arcGISFeatureLayer = (ArcGISFeatureLayer) obj;
                arcGISFeatureLayer.setVisible(false);
                KMLLayer.this.addLayer(arcGISFeatureLayer);
                if (arcGISFeatureLayer.getGeometryType() == Geometry.Type.POINT) {
                    KMLLayer.this.d = arcGISFeatureLayer;
                } else if (arcGISFeatureLayer.getGeometryType() == Geometry.Type.POLYLINE) {
                    KMLLayer.this.e = arcGISFeatureLayer;
                } else if (arcGISFeatureLayer.getGeometryType() == Geometry.Type.POLYGON) {
                    KMLLayer.this.f = arcGISFeatureLayer;
                } else {
                    throw new RuntimeException("No Geometry type is defined for the feature layer");
                }
            } else if (obj instanceof d) {
                d dVar = (d) obj;
                String d = dVar.d();
                if (dVar.c() == null) {
                    KMLLayer kMLLayer = new KMLLayer(d, KMLLayer.this.getDefaultSpatialReference(), dVar, true);
                    kMLLayer.y = KMLLayer.this;
                    KMLLayer.this.addLayer(kMLLayer);
                    KMLLayer.this.w.add(kMLLayer);
                    return;
                }
                KMLLayer.this.v.add(dVar);
            } else if (obj instanceof com.esri.core.internal.value.c) {
                com.esri.core.internal.value.c cVar = (com.esri.core.internal.value.c) obj;
                a aVar = new a(cVar.e(), cVar.d(), cVar.c(), cVar.f());
                aVar.a(cVar.h());
                aVar.a(cVar.g());
                if (KMLLayer.this.x == null) {
                    KMLLayer.this.x = new ArrayList();
                }
                com.esri.android.map.ogc.b bVar = new com.esri.android.map.ogc.b();
                KMLLayer.this.addLayer(bVar);
                KMLLayer.this.x.add(bVar);
                bVar.setVisible(cVar.i());
                bVar.a((com.esri.android.map.ogc.a) aVar);
            }
        }
    }

    @Override // com.esri.android.map.GroupLayer, com.esri.android.map.Layer
    public boolean isInitialized() {
        return this.c.get();
    }

    private void i() {
        if (this.g != null) {
            if (isVisible()) {
                if (this.z.get() == 2) {
                    String i = this.g.i();
                    int h = this.g.h();
                    this.t = this.g.k();
                    this.u = this.g.l();
                    if (i == null || i.toLowerCase().indexOf("oninterval") == -1 || h <= 0) {
                        return;
                    }
                    Timer timer = this.s;
                    if (timer != null) {
                        timer.cancel();
                    }
                    Timer timer2 = new Timer(false);
                    this.s = timer2;
                    long j = h * 1000;
                    timer2.scheduleAtFixedRate(new TimerTask() { // from class: com.esri.android.map.ogc.KMLLayer.3
                        @Override // java.util.TimerTask, java.lang.Runnable
                        public void run() {
                            KMLLayer.this.d();
                        }
                    }, j, j);
                    return;
                }
                return;
            }
            Timer timer3 = this.s;
            if (timer3 != null) {
                timer3.cancel();
            }
        }
    }

    @Override // com.esri.android.map.Layer
    public void setVisible(boolean z) {
        super.setVisible(z);
        i();
    }

    @Override // com.esri.android.map.GroupLayer, com.esri.android.map.Layer
    public void recycle() {
        super.recycle();
        Timer timer = this.s;
        if (timer != null) {
            timer.cancel();
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public static class a extends com.esri.android.map.ogc.a {
        int i;
        String j;

        public a(String str, Envelope envelope, SpatialReference spatialReference, int i) {
            super(str, envelope, spatialReference);
            this.i = i;
        }

        int h() {
            return this.i;
        }

        public void a(String str) {
            this.j = str;
        }

        public String i() {
            return this.j;
        }
    }
}
